﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNS_BLL;
using QLNS_Public;

namespace QLNS_GiaoDien
{
    public partial class uc_LUONG : UserControl
    {
        public uc_LUONG()
        {
            InitializeComponent();
        }

        Luong_BLL luong_bll = new Luong_BLL();
        Luong_Public l = new Luong_Public();

        private void cob_MaCong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void txt_MaLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void cob_MaCong_TextChanged(object sender, EventArgs e)
        {
            string sql = "select  tbl_NhanVien.MaNhanVien,tbl_NhanVien.HoTen,tbl_PhongBan.TenPhong from tbl_PhongBan,tbl_NhanVien,tbl_ChamCong where tbl_NhanVien.MaPhong=tbl_PhongBan.MaPhong and tbl_NhanVien.MaNhanVien=tbl_ChamCong.MaNhanVien and tbl_ChamCong.MaCong='"+cob_MaCong.Text+"'";
            DataTable dt = new DataTable();
            dt = luong_bll.taoBang(sql);
            cob_MaNV.DataSource = dt;
            cob_MaNV.DisplayMember = "MaNhanVien";
            cob_TenNV.DataSource = dt;
            cob_TenNV.DisplayMember = "HoTen";
            cob_PhongBan.DataSource = dt;
            cob_PhongBan.DisplayMember = "TenPhong";
            string sql_thuclanh = "select ((tbl_BacLuong.HeSo*tbl_BacLuong.LuongCoBan)-(tbl_BacLuong.LuongCoBan/22)*tbl_ChamCong.NgayNghiKhongPhep) as 'Thực Lãnh' FROM           dbo.tbl_Luong INNER JOIN dbo.tbl_ChamCong ON dbo.tbl_Luong.MaCong = dbo.tbl_ChamCong.MaCong INNER JOIN  dbo.tbl_NhanVien ON dbo.tbl_ChamCong.MaNhanVien = dbo.tbl_NhanVien.MaNhanVien INNER JOIN  dbo.tbl_BacLuong ON dbo.tbl_NhanVien.MaBacLuong = dbo.tbl_BacLuong.MaBacLuong";
            DataTable dt1 = new DataTable();
            dt1 = luong_bll.taoBang(sql_thuclanh);
            cob_ThucLanh.DataSource = dt1;
            cob_ThucLanh.DisplayMember = "Thực Lãnh";
        }

        private void uc_LUONG_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = luong_bll.LayDuLieu();
        }
        int flag;
        private void btn_Them_Click(object sender, EventArgs e)
        {
            flag = 0;
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            flag = 1;
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            string s;
            l.MaLuong = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Lương"));
            s = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Họ Và Tên").ToString();
            if (MessageBox.Show("Bạn có muốn xóa Mã Lương: '" + l.MaLuong + "' của Nhân viên: '" + s + "' này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                luong_bll.delete_Luong(l);
                uc_LUONG_Load(sender, e);
            }
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                l.MaCong = int.Parse(cob_MaCong.Text);
                l.Thuong = float.Parse("0"+txt_Thuong.Text);
                l.PhuCap = float.Parse("0"+txt_Phucap.Text);
                l.TienBaoHiem = float.Parse("0"+txt_Tienbaohiem.Text);
                l.GiamTruKhac = float.Parse("0"+txt_Giamtrukhac.Text);
                l.NgayLap = Convert.ToDateTime(dateTime_NgayLap.Text);
                l.NguoiLap = txt_Nguoilap.Text;
                l.GhiChu = txt_Ghichu.Text;
                luong_bll.insert_Luong(l,l,l,l,l,l,l,l);
                MessageBox.Show("Thêm mới thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            if (flag == 1)
            {
                l.MaLuong = int.Parse(txt_MaLuong.Text);
                l.MaCong = int.Parse(cob_MaCong.Text);
                l.Thuong = float.Parse("0"+txt_Thuong.Text);
                l.PhuCap = float.Parse("0"+txt_Phucap.Text);
                l.TienBaoHiem = float.Parse("0"+txt_Tienbaohiem.Text);
                l.GiamTruKhac = float.Parse("0"+txt_Giamtrukhac.Text);
                l.NgayLap = Convert.ToDateTime(dateTime_NgayLap.Text);
                l.NguoiLap = txt_Nguoilap.Text;
                l.GhiChu = txt_Ghichu.Text;
                luong_bll.Update_Luong(l,l, l, l, l, l, l, l, l);
                MessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txt_Phucap_TextChanged(object sender, EventArgs e)
        {
            txt_Tienbaohiem_TextChanged(sender,e);
        }

        private void txt_Thuong_TextChanged(object sender, EventArgs e)
        {
            txt_Tienbaohiem_TextChanged(sender,e);
        }

        private void txt_Giamtrukhac_TextChanged(object sender, EventArgs e)
        {
            txt_Tienbaohiem_TextChanged(sender,e);
        }

        private void txt_Tienbaohiem_TextChanged(object sender, EventArgs e)
        {
            try
            {
                float th = float.Parse(txt_Thuong.Text), bh = float.Parse(txt_Tienbaohiem.Text), gtru = float.Parse(txt_Giamtrukhac.Text), pc = float.Parse(txt_Phucap.Text), tl = float.Parse(cob_ThucLanh.Text), kq;
                kq = tl + th - bh - gtru + pc;
                cob_ThucLanh.Text = kq.ToString();
            }
            catch (Exception)
            { }
        }
    }
}
